package com.ruoyi.cderp.service.impl;

import java.util.List;

import com.ruoyi.cderp.domain.CategoryProperty;
import com.ruoyi.cderp.mapper.CategoryPropertyMapper;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.cderp.mapper.ProductPropertyMapper;
import com.ruoyi.cderp.domain.ProductProperty;
import com.ruoyi.cderp.service.IProductPropertyService;
import org.springframework.transaction.annotation.Transactional;

import static com.ruoyi.common.utils.SecurityUtils.getLoginUser;

/**
 * 商品属性Service业务层处理
 *
 * @author ruoyi
 * @date 2025-03-05
 */
@Service
public class ProductPropertyServiceImpl implements IProductPropertyService
{
    @Autowired
    private ProductPropertyMapper productPropertyMapper;

    @Autowired
    private CategoryPropertyMapper categoryPropertyMapper;

    /**
     * 查询商品属性
     *
     * @param id 商品属性主键
     * @return 商品属性
     */
    @Override
    public ProductProperty selectProductPropertyById(String id)
    {
        return productPropertyMapper.selectProductPropertyById(id);
    }

    /**
     * 查询商品属性列表
     *
     * @param productProperty 商品属性
     * @return 商品属性
     */
    @Override
    public List<ProductProperty> selectProductPropertyList(ProductProperty productProperty)
    {
        return productPropertyMapper.selectProductPropertyList(productProperty);
    }

    /**
     * 新增商品属性
     *
     * @param productProperty 商品属性
     * @return 结果
     */
    @Transactional
    @Override
    public int insertProductProperty(ProductProperty productProperty)
    {
        productProperty.setCreateTime(DateUtils.getNowDate());

        // 获取当前登录用户对象
        LoginUser loginUser = SecurityUtils.getLoginUser();
        //创建人编号
        productProperty.setCreateById(loginUser.getUser().getUserId().toString());
        //创建人名称
        productProperty.setCreateBy(loginUser.getUser().getUserName());

        int num = productPropertyMapper.insertProductProperty(productProperty);

        if(productProperty.getPropertyType()==2){
            //插入关联表
            CategoryProperty categoryProperty = new CategoryProperty();
            categoryProperty.setPropertyId(productProperty.getId());
            categoryProperty.setCategoryId(productProperty.getCategoryId());
            categoryPropertyMapper.insertCategoryProperty(categoryProperty);
        }
        return num;
    }


    /**
     * 修改商品属性
     *
     * @param productProperty 商品属性
     * @return 结果
     */
    @Transactional
    @Override
    public int updateProductProperty(ProductProperty productProperty)
    {
        productProperty.setUpdateTime(DateUtils.getNowDate());

        // 获取当前登录用户对象
        LoginUser loginUser = SecurityUtils.getLoginUser();
        //修改人编号
        productProperty.setUpdateById(loginUser.getUser().getUserId().toString());
        //修改人名称
        productProperty.setUpdateBy(loginUser.getUser().getUserName());

        int num = productPropertyMapper.updateProductProperty(productProperty);

        //是否为属性指定类目==》2指定类目
        if(productProperty.getPropertyType()==2){
            //查询此属性是否存在商品类目
            CategoryProperty  categoryPropertylist= categoryPropertyMapper.selectCategoryPropertyPId(productProperty.getId());
            //存在则修改不存在则插入
            if(categoryPropertylist!=null){
                CategoryProperty categoryProperty = new CategoryProperty();
                categoryProperty.setPropertyId(productProperty.getId());
                categoryProperty.setCategoryId(productProperty.getCategoryId());
                categoryPropertyMapper.updateCategoryProperty(categoryProperty);
            }else{
                //插入关联表
                CategoryProperty categoryProperty = new CategoryProperty();
                categoryProperty.setPropertyId(productProperty.getId());
                categoryProperty.setCategoryId(productProperty.getCategoryId());
                categoryPropertyMapper.insertCategoryProperty(categoryProperty);
            }
        }else{
            //删除类目
            categoryPropertyMapper.deleteCategoryPropertyBypId(String.valueOf(productProperty.getId()));
        }
        return num;
    }

    /**
     * 批量删除商品属性
     *
     * @param ids 需要删除的商品属性主键
     * @return 结果
     */
    @Override
    public int deleteProductPropertyByIds(String[] ids)
    {
        return productPropertyMapper.deleteProductPropertyByIds(ids);
    }

    /**
     * 删除商品属性信息
     *
     * @param id 商品属性主键
     * @return 结果
     */
    @Override
    public int deleteProductPropertyById(String id)
    {
        return productPropertyMapper.deleteProductPropertyById(id);
    }
}
